<!DOCTYPE html>
<html>

<head lang="en">
    <title>找攻略</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategy.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/common.js"></script>
    <script>
        $(function () {
            //获取当前最近一篇攻略推荐数据
            $.get("/travels/commends", {type: 3, pageSize: 1}, function (data) {
                //使用jrender渲染数据到推荐位置
                $(".commend").renderValues(data.list[0], {
                    getHref: getHref
                });
                //$(".carousel-item:first").addClass("active");
            });

            //获取所有状态为推荐的攻略
            $.get("/strategies",{state:2},function (data) {
                $(".strategyCommend").renderValues(data,{
                    getHref: getHref
                })
            });
            //当前选中的推荐地区的id
            var regionId;
            //获取所有的推荐地区，并获取所有的地区的数据，把基本结构放到相应的位置
            $.get("/regions",{state:1},function (data) {
                var title = "";
                var content = "";
                $.each(data,function (index, ele) {
                    title += '<li class="nav-item"><a class="nav-link" data-rid="'+ele.id+'" data-toggle="pill" href="#pills-'+ele.id+'">'+ele.name+'</a></li>';
                    content += '<div class="tab-pane fade" id="pills-'+ele.id+'"></div>';
                })
                $("#pills-tab").append(title);
                $("#pills-tabContent").append(content);
                //推荐地区绑定点击事件，使用freemarker模板渲染数据，添加到div中
                $(".nav-link[data-rid]").click(function () {
                    //发送请求获取所有的游记信息
                    currentPage = 2;
                    //定义总页数
                    pages=2;
                    //获取选中的id
                    regionId=$(this).data("rid");
                    //发送请求，获取选中id的数据信息（当季推荐和所有攻略）
                    $.get("/regions/"+regionId+"/strategies",function (data) {
                        $("#pills-"+regionId).html(data);
                    },"html");
                });

                //发送请求获取所有的游记信息
                var currentPage = 2;
                //定义总页数
                var pages=2;
                //定义查询状态
                var loading = false;
                function query() {
                    if (loading) {
                        return;
                    }
                    loading = true;

                    $.get("/regions/"+regionId+"/strategies",{currentPage:currentPage},function (data) {
                        pages = data.pages;


                        //拿到信息使用json对象，遍历后拼在后面
                        $.each(data.list, function (index, ele) {
                            //拿到模板
                            var div=$("#template .mb").clone(true);
                            //设置信息
                            $(div).find("img").attr("src", ele.coverUrl);
                            $(div).find("span").html(ele.title);
                            $(".classify").append(div);
                        });
                        loading = false;
                        currentPage++;
                    },"json");
                }

                //绑定滚动事件
                $(window).scroll(function () {
                    if ($(window).height()+$(document).scrollTop()+0.5>=$(document).height()) {
                        //第二次查询
                        if(currentPage<=pages&&regionId) {
                            query();
                        }else {
                            $(document).dialog({
                                type : 'notice',
                                infoText: '亲，已经到底了，不要再划拉',
                                autoClose: 2500,
                                position: 'bottom'  // center: 居中; bottom: 底部
                            });
                        }
                    }
                });
            });





        });
    </script>

</head>

<body>
<!--分页时使用的模板-->
<div id="template" style="display: none">
    <div class="col-6 mb">
        <a >
            <img class="float-left" >
            <div class="classify-text">
                <span></span>
                <p>0人收藏</p>
            </div>
        </a>
    </div>
</div>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="index.html">
                <span><i class="fa fa-chevron-left fa-2x"></i></span>
            </a>
        </div>
        <div class="col-10">
            <div class="input-group-sm search">
                <input class="form-control searchBtn" placeholder="找攻略">
            </div>
        </div>
    </div>
</div>

<ul class="nav nav-pills nav-justified border border-left-0 border-top-0 border-right-0" id="pills-tab" role="tablist">
    <li class="nav-item">
        <a class="nav-link active show" data-toggle="pill" href="#pills-top">
            <span style="font-weight: bold;">推荐</span>
        </a>
    </li>
</ul>

<div class="tab-content" id="pills-tabContent">
    <div class="tab-pane fade active show" id="pills-top">
        <div class="container commend">
            <a data-url="travelContent.html" render-key="travel_id" render-fun="getHref">
                <img render-src="coverUrl">
                <p render-html="title"></p>
            </a>
        </div>
        <hr>
        <div class="container strategyCommend">
            <h6>当季推荐</h6>
            <div class="row hot" render-loop="list">
                <div class="col-4">
                    <a data-url="strategyCatalogs.html" render-key="list.id" render-fun="getHref">
                        <img render-src="list.coverUrl">
                        <p render-html="list.title"></p>
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>